﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lab1
{

    public partial class Form1 : Form
    {
        string A;
        string B;
        string C;
        string Checker;
        string Alphabet;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            A = "";
            B = "";
            C = "";
            Checker = "";
            Alphabet = "0123456789";
        }

        private void RightButt_Click(object sender, EventArgs e)
        {
            A = ""; B = ""; C = "";
            listBox1.Items.Clear();
            A = "A := 0" + textBox1.Text + " | " + "1" + textBox4.Text;
            B = "B := 0" + textBox2.Text + " | " + "1" + textBox5.Text;
            C = "C := 0" + textBox3.Text + " | " + "1" + textBox6.Text;
            listBox1.Items.Add(A);
            listBox1.Items.Add(B);
            listBox1.Items.Add(C);
        }

        private void LeftButt_Click(object sender, EventArgs e)
        {
            A = ""; B = ""; C = "";
            listBox2.Items.Clear();
            Grammar("A");
            Grammar("B");
            Grammar("C");
            listBox2.Items.Add(A);
            listBox2.Items.Add(B);
            listBox2.Items.Add(C);

        }
        public void Grammar(string determinal)
        {
            bool empty = false;
            string Str = determinal + " :=";
            if (textBox1.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "A" + "0 ";
                empty = true;
            }
            if (textBox2.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "B" + "0 ";
                empty = true;
            }
            if (textBox3.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "C" + "0 ";
                empty = true;
            }
            if (textBox4.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "A" + "1 ";
                empty = true;
            }
            if (textBox5.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "B" + "1 ";
                empty = true;
            }
            if (textBox6.Text == determinal)
            {
                if (empty) Str += " | ";
                Str += "C" + "1 ";
                empty = true;
            }
            
            if (empty) listBox2.Items.Add(Str);

        }



        private void button3_Click_1(object sender, EventArgs e)
        {
            int currentState = 0;
            const int stateA = 0;
            const int stateB = 1;
            const int stateC = 2;
            const int stateD = 3;
            Checker = textBox9.Text;
            for (int i = 0; i < Checker.Length; i++)
            {
                switch (currentState)
                {
                    case stateA:
                        if (Checker[i] == '0')
                            currentState = stateB;
                        break;

                    case stateB:
                        if (Checker[i] == '0')
                            currentState = stateC;
                        else
                            currentState = stateA;
                        break;

                    case stateC:
                        if (Checker[i] == '0')
                            currentState = stateD;
                        break;

                    case stateD:
                        if (Checker[i] == '0')
                            currentState = stateA;
                        else
                            currentState = stateC;
                        break;
                }
            }

            if (currentState == stateA || currentState == stateB)
                textBox10.Text = "ERROR";
            if (currentState == stateC || currentState == stateD)
                textBox10.Text = "ALL COOL";
        }

     
         private void button1_Click(object sender, EventArgs e)
              {
                  Checker = "";
                  int currentState = 0;                 
                  const int stateA = 0;
                  const int stateB = 1;
                  const int stateC = 2;
                  const int stateD = 3;
                  const int stateG = 4;
                  const int stateF = 6;
                  
                  Checker = textBox11.Text;
                  for (int i = 0; i < Checker.Length; i++)
                  {
                      switch (currentState)
                      {
                          case stateA:
                              if (Alphabet.Contains(Checker[i]))
                                  currentState = stateC;
                              else
                                  if (Checker[i] == '-')
                                      currentState = stateB;
                                  else
                                      currentState = stateF;
                              break;

                          case stateB:
                              if (Alphabet.Contains(Checker[i]))
                                  currentState = stateC;
                              else
                                  currentState = stateF;
                              break;
                              
                          case stateC:
                              if (!Alphabet.Contains(Checker[i]))                                  
                                  if (Checker[i] == '.')
                                      currentState = stateD;
                                  else
                                      currentState = stateF;
                              break;

                          case stateD:
                              if (Alphabet.Contains(Checker[i]))
                                  currentState = stateG;
                              else
                                  currentState = stateF;
                              break;

                          case stateG:
                              if (!Alphabet.Contains(Checker[i]))
                                  currentState = stateF;
                              break;
                      }
                  }

                  if (currentState == stateG)
                      textBox12.Text = "ALL COOL";
                  else
                      textBox12.Text = "ERROR";
              }
         }
}
